
package com.showcai.controller.certificate;

import com.showcai.controller.base.BaseController;
import com.showcai.module.Certificate;
import com.showcai.module.ProductEntity;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.CertificatePackService;
import com.showcai.service.CertificateService;
import com.showcai.service.ProductEntityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by lutao on 2017/7/14.
 */
@RequestMapping("/certificatePack")
@Controller
public class CertificatePackController extends BaseController {
    @Autowired
    CertificatePackService certificatePackService;
    @Autowired
    private ProductEntityService productEntityCertificateService;
    @Autowired
    CertificateService certificateService;
    /**
     * 获取卡包里面的卡券列表
     *
     * @return
     */
    @RequestMapping(value = "/getCertificatePackList/{memberId}", method = RequestMethod.POST)
    @ResponseBody
    public String getCertificatePackList(@PathVariable String memberId) {
        ResultObj resultObj = new ResultObj();
      List<Certificate> certificateList=  certificatePackService.getCertificateListFromPackByMemberId(memberId);
        resultObj.setData(certificateList);
        resultObj.setMsg("操作成功！");

        return resultObj.toJson();
    }

    /**
     * 获取卡券详细信息
     * @param certificateId
     * @return
     */
    @RequestMapping(value = "/getCertificate/{certificateId}", method = RequestMethod.POST)
    @ResponseBody
    public String getCertificate(@PathVariable String certificateId) {
        ResultObj resultObj = new ResultObj();

         Certificate certificate=certificateService.getCertificateByCertificateId(certificateId);
         if(certificate==null){
             throw new MyRunTimeExcption(ReturnCode.CERTIFICATE_IS_INEXISTENCE);
         }
        ProductEntity productEntity = productEntityCertificateService.getProductEntityById(certificate.getProductEntityId());
        resultObj.setData(productEntity);
        resultObj.setMsg("操作成功！");

        return resultObj.toJson();
    }


}
